package com.yx.front.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yx.front.config.global.BizException;
import com.yx.front.config.global.CommonResult;
import com.yx.front.model.BookCollect;
import com.yx.front.model.BookMessage;
import com.yx.front.model.SysUser;
import com.yx.front.model.param.CollectParam;
import com.yx.front.model.vo.BookMessageVo;
import com.yx.front.service.BookCollectService;
import com.yx.front.service.BookMessageService;
import com.yx.front.service.SysUserService;
import com.yx.front.utils.LoginUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.yx.front.converter.BookMessageConverter.handleResult;

/**
 * @author YangXiong
 */
@Component
public class BookCollectManager {
    @Resource
    private BookMessageService bookMessageService;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private BookCollectService bookCollectService;
    @Resource
    private LoginUtil loginUtil;

    public CommonResult<String> collect(CollectParam param) {
        SysUser currentUser = loginUtil.getCurrentUser();
        LambdaQueryWrapper<BookCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BookCollect::getBookId, param.getBookId())
                .eq(BookCollect::getUserId, currentUser.getId());
        BookCollect one = bookCollectService.getOne(wrapper);
        BookCollect del = bookCollectService.getLogicOne(currentUser.getId(), param.getBookId());
        if (Objects.isNull(one)) {
            if (Objects.isNull(del)) {
                BookCollect collect = new BookCollect();
                collect.setBookId(param.getBookId());
                collect.setCreateTime(DateUtil.date());
                collect.setModifyTime(DateUtil.date());
                collect.setOperatorId(currentUser.getId());
                collect.setOperatorName(currentUser.getUserName());
                collect.setUserId(currentUser.getId());
                boolean save = bookCollectService.save(collect);
                if (save) {
                    return CommonResult.success("收藏成功！");
                }
            } else {
                boolean update = bookCollectService.updateLogicDelete(currentUser.getId(), param.getBookId());
                if (update) {
                    return CommonResult.success("收藏成功！");
                }
            }
            return CommonResult.fail("收藏失败！");
        }
        boolean remove = bookCollectService.removeById(one.getId());
        if (remove) {
            return CommonResult.success("取消收藏成功！");
        }
        return CommonResult.fail("取消收藏失败！");
    }

    public CommonResult<List<BookMessageVo>> getByUserName(String userName) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUserName, userName);
        SysUser one = sysUserService.getOne(queryWrapper);
        if (Objects.isNull(one)) {
            throw new BizException("当前用户不存在");
        }
        //查询书籍信息
        LambdaQueryWrapper<BookCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BookCollect::getUserId, one.getId());
        List<BookCollect> list = bookCollectService.list(wrapper);
        List<Long> bookIds = list.stream().map(BookCollect::getBookId).collect(Collectors.toList());
        if(CollectionUtil.isEmpty(bookIds)){
            return CommonResult.success(Collections.emptyList());
        }
        List<BookMessage> messageList = bookMessageService.listByIds(bookIds);
        return CommonResult.success(handleResult(messageList));
    }
}
